package com.welson.db.cryptor.util;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 加密处理器
 * @author welson
 */
public class EncryptHandler {
    
    /**
     * 处理加密
     */
    public static Object encrypt(Object val) {
        if (val == null) {
            return null;
        }
        if (!(val instanceof String)) {
            throw new IllegalArgumentException("Only supports String type for encryption");
        }
        return Cryptor.encrypt((String) val);
    }

    /**
     * 处理集合加密
     */
    public static Collection<?> encryptCollection(Collection<?> coll) {
        if (coll == null || coll.isEmpty()) {
            return coll;
        }
        if (!(coll.iterator().next() instanceof String)) {
            throw new IllegalArgumentException("Only supports String type for encryption");
        }
        return coll.stream()
                .map(item -> Cryptor.encrypt((String) item))
                .collect(Collectors.toList());
    }
}